package IOService;

import dao.CommodityDao;
import pojo.Commodity;
import pojo.CommodityList;

import java.util.Scanner;

public class AddService implements IOService {
    @Override
    public void work(CommodityList commodityList) {
        Scanner s = new Scanner(System.in);
        System.out.println("编号");
        int id = s.nextInt();
        System.out.println("输入名字");
        String name = s.next();
        System.out.println("日期");
        String data = s.next();
        System.out.println("保质期");
        String expirationData = s.next();
        System.out.println("生厂商");
        String manufacturer = s.next();
        System.out.println("价格");

        int price = s.nextInt();
        Commodity commodity = new Commodity(id, name, data, expirationData, manufacturer, price);
        for (int i = 0; i < commodityList.getCommdityCount(); i++) {
            Commodity commodities = commodityList.getCommodities(i);
            if (commodities.getName().equals(name)) {
                System.out.println("物品存在");
                return;
            }
        }
        commodityList.setCommodities(commodity);
        commodityList.setCommdityCount(commodityList.getCommdityCount() + 1);
        CommodityDao b = new CommodityDao();
        int add = b.Add(commodity);
        if (add == 1) {
            System.out.println("添加成功");

        }
    }
}